angular.module('ngBoilerplate.news',['ui.router'])
.config(function($stateProvider) {
	$stateProvider.state('news',{
		url:'/news',
		views:{
			'main':{
				templateUrl:'news/news.tpl.html',
				controller:'newsCtrl'
			}
		},
		data: {pageTitle: 'News'}
	});
})

.controller('newsCtrl', ['$scope', '$http', function($scope,$http){
	$http({
		method: 'GET',
		url: '../src/app/news/news.json'
	}).success(function(data, status, headers, config){
		$scope.newsList=data.news;
	}).error(function(data, status, headers, config){
        alert(status+" error,request failed!");
	});
}])

.directive('newsScroller',['$interval',function($interval) {
    return{
        scope: {
            news : '=',
            delay : '@'
        },
        restrict: 'EA',
        transclude: true,
        templateUrl: '../src/app/news/showNews.tpl.html',
        link: function(scope,element,attrs){
            var scroller;
            var ulElem;
            function startScroller() {
                ulElem=element.find('ul').eq(0);//选取div下的第一个ul
                ulElem.append(ulElem.find('li').eq(0));//获取li并插入ul中
            }

            scroller=$interval(startScroller,scope.delay); //页面载入时自动触发滚动新闻

            element.bind('mouseover',function(){
                $interval.cancel(scroller);
            });

            element.bind('mouseout',function(){
                scroller=$interval(startScroller,scope.delay);
            });
        }
    };

}]);